package serviceswarm

import (
	"context"
	"github.com/aberic/gnomon"
	"github.com/pkg/errors"
	"google.golang.org/grpc"
	"gykjgit.dccnet.com.cn/chain/proto/swarm"
)

// StackDeploy StackDeploy
func StackDeploy(url string, in *swarm.ReqCompose) (*swarm.Response, error) {
	resp, err := gnomon.GRPCRequestPools(url, func(conn *grpc.ClientConn) (i interface{}, err error) {
		// 创建grpc客户端
		cli := swarm.NewStackServerClient(conn)
		// 客户端向grpc服务端发起请求
		return cli.Deploy(context.Background(), in)
	})
	if nil == err {
		if response, ok := resp.(*swarm.Response); ok {
			return response, nil
		}
		return nil, errors.New("stack deploy failed, can not tarns interface to response")
	}
	return nil, errors.Wrap(err, "stack deploy failed")
}

// StackList StackList
func StackList(url string) (*swarm.RespStackList, error) {
	resp, err := gnomon.GRPCRequestPools(url, func(conn *grpc.ClientConn) (i interface{}, err error) {
		// 创建grpc客户端
		cli := swarm.NewStackServerClient(conn)
		// 客户端向grpc服务端发起请求
		return cli.List(context.Background(), &swarm.Request{})
	})
	if nil == err {
		if response, ok := resp.(*swarm.RespStackList); ok {
			return response, nil
		}
		return nil, errors.New("stack list failed, can not tarns interface to response")
	}
	return nil, errors.Wrap(err, "stack list failed")
}

// StackProcess StackProcess
func StackProcess(url, stackName string) (*swarm.RespStackProcesses, error) {
	resp, err := gnomon.GRPCRequestPools(url, func(conn *grpc.ClientConn) (i interface{}, err error) {
		// 创建grpc客户端
		cli := swarm.NewStackServerClient(conn)
		// 客户端向grpc服务端发起请求
		return cli.Process(context.Background(), &swarm.ReqStack{StackName: stackName})
	})
	if nil == err {
		if response, ok := resp.(*swarm.RespStackProcesses); ok {
			return response, nil
		}
		return nil, errors.New("stack process failed, can not tarns interface to response")
	}
	return nil, errors.Wrap(err, "stack process failed")
}

// StackServices StackServices
func StackServices(url, stackName string) (*swarm.RespStackServices, error) {
	resp, err := gnomon.GRPCRequestPools(url, func(conn *grpc.ClientConn) (i interface{}, err error) {
		// 创建grpc客户端
		cli := swarm.NewStackServerClient(conn)
		// 客户端向grpc服务端发起请求
		return cli.Services(context.Background(), &swarm.ReqStack{StackName: stackName})
	})
	if nil == err {
		if response, ok := resp.(*swarm.RespStackServices); ok {
			return response, nil
		}
		return nil, errors.New("stack services failed, can not tarns interface to response")
	}
	return nil, errors.Wrap(err, "stack services failed")
}

// StackRemove StackRemove
func StackRemove(url, stackName string) (*swarm.Response, error) {
	resp, err := gnomon.GRPCRequestPools(url, func(conn *grpc.ClientConn) (i interface{}, err error) {
		// 创建grpc客户端
		cli := swarm.NewStackServerClient(conn)
		// 客户端向grpc服务端发起请求
		return cli.Remove(context.Background(), &swarm.ReqStack{StackName: stackName})
	})
	if nil == err {
		if response, ok := resp.(*swarm.Response); ok {
			return response, nil
		}
		return nil, errors.New("stack remove failed, can not tarns interface to response")
	}
	return nil, errors.Wrap(err, "stack remove failed")
}
